package com.chushouya.manager.dao.repository;


import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.manager.dao.entity.ProgramEntity;
import com.chushouya.manager.dao.mapper.ProgramMapper;
import com.chushouya.manager.dto.admin.program.ProgramQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Objects;

@Repository
public class ProgramRepository extends BaseRepository<ProgramMapper, ProgramEntity, Long> {

    public ProgramEntity selectByAppIdAndPlatform(String appId, String platform) {
        Example example = buildExample();
        example.and().andEqualTo("appId", appId).andEqualTo("platform", platform);
        return super.selectFirstOneByExample(example);
    }

    public ProgramEntity selectByAppId(String appId) {
        Example example = buildExample();
        example.and().andEqualTo("appId", appId);
        return super.selectFirstOneByExample(example);
    }

    public ProgramEntity selectOne(ProgramQuery programQuery) {
        Example example = buildExample(programQuery);
        return super.selectFirstOneByExample(example);
    }


    private Example buildExample(ProgramQuery programQuery) {
        Example example = buildExample();
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(programQuery.getAppId())) {
            criteria.andEqualTo("appId", programQuery.getAppId());
        }
        return example;
    }
}
